
import com.jme3.bullet.BulletAppState;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.math.ColorRGBA;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.nifty.tools.SizeValue;
import java.util.ArrayList;

/**
 *
 * @author David Zhang
 */
public class Gui implements ScreenController{
    ArrayList<BitmapText> chatLog = new ArrayList<BitmapText>();
    Element progressBarElement;
    int loadcount = 1, loaded = 0;
    LoadGame loader;
    Nifty nifty;
    NiftyJmeDisplay niftyDisplay;
    TextRenderer textRenderer; //Declares a text renderer
    
    public Gui(BitmapFont guiFont){
        niftyDisplay = new NiftyJmeDisplay(Main.app.getAssetManager(),
                                           Main.app.getInputManager(),
                                           Main.app.getAudioRenderer(), 
                                           Main.app.getGuiViewPort()); //Constructs the GUI display
        nifty = niftyDisplay.getNifty(); //Sets the nifty
        nifty.fromXml("Interface/nifty_loading.xml", "start", this); //Loads the XML file
        Main.app.getGuiViewPort().addProcessor(niftyDisplay); //Starts the nifty
        populateChat(Main.app.getGuiNode(), guiFont);
    }
    
    public void chatAdd(String input) {
        for(int i = 0; i < 34; i++) chatLog.get(i).setText(chatLog.get(i + 1).getText());
        chatLog.get(34).setText(input);
    }
    
    public void loadStep(){
        if(loadcount == 1){
            loader = new LoadGame();
            loader.step1();
        }
        else if(loadcount == 2) loader.step2();
        else if(loadcount == 3) loader.step3();
        else if(loadcount == 4) loader.step4();
        else if(loadcount == 5) loader.step5();
        else if(loadcount == 6) loader.step6();
        else if(loadcount == 7) loader.step7();
        else if(loadcount == 8) loader.step8();
        loadcount++;
    }

    private void populateChat(Node guiNode, BitmapFont guiFont) {
        for(int i = 0; i < 35; i++){
            chatLog.add(new BitmapText(guiFont, false));
            chatLog.get(i).setSize(guiFont.getCharSet().getRenderedSize());      // font size
            chatLog.get(i).setColor(ColorRGBA.White);                             // font color
            chatLog.get(i).setText("");             // the text
            chatLog.get(i).setLocalTranslation(0, Main.appSettings.getHeight() - ((chatLog.get(i).getLineHeight() * i) + 5), 0); // position
            guiNode.attachChild(chatLog.get(i));
        }
    }
    
    public void setProgress(final float progress, String loadingText) {
        final int MIN_WIDTH = 32;
        int pixelWidth = (int) (MIN_WIDTH + (progressBarElement.getParent().getWidth() - MIN_WIDTH) * progress);
        progressBarElement.setConstraintWidth(new SizeValue(pixelWidth + "px"));
        progressBarElement.getParent().layoutElements();
        textRenderer.setText(loadingText);
    }
    
    public void showLoadingMenu() {
        System.out.println("loading");
        Main.app.name = nifty.getCurrentScreen().findNiftyControl("input", TextField.class).getText();
        Main.app.client.send(Main.app.name + ": has entered the game");
        nifty.gotoScreen("loadlevel");
        loaded = 1;
    }
    
    public void bind(Nifty nifty, Screen screen) {
        progressBarElement = nifty.getScreen("loadlevel").findElementByName("progressbar");
    }
    public void onStartScreen() {}
    public void onEndScreen() {}
}